package com.fsy.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fsy.entities.Message;
import com.fsy.service.MessageService;
import com.fsy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/5/2 14:32
 */
@RequestMapping("/message")
@RestController
@CrossOrigin
public class MessageController {
    @Resource
    private MessageService messageService;

    /**
     * 全查消息
     * @param message
     * @return
     */
    @GetMapping("/selectAllMessage")
    public Map<String,Object> selectAllMessage(Message message){
        List<Message> list = messageService.selectAllByReceiverId(message);
        int count = messageService.getCount(message);
        return Result.toClient("0","成功",count,list);
    }

    /**
     * 把消息设为已读
     * @param id
     * @return
     */
    @PostMapping("updateRead")
    public Map<String ,Object> updateRead(Integer id){
        UpdateWrapper<Message> wrapper = new UpdateWrapper<>();
        wrapper.set("isRead",1).eq("id",id);
        boolean update = messageService.update(wrapper);
        return update==true ? Result.toClient("0","修改成功"): Result.toClient("1","修改失败");
        /*if (update == true){
            return Result.toClient("0","修改成功");
        }else {
            return Result.toClient("0","修改失败");
        }*/
    }

    /**
     * 查询未读消息
     * @param
     * @return
     */
    @GetMapping("getIsNoReadMessage")
    public boolean getIsNoReadMessage(Integer userId){
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiverId",userId).eq("isRead",2);
        return messageService.count(queryWrapper) > 0 ? true : false;
    }





















}
